﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using LinkCard.AdminCP.Common;
using LinkCard.Model;
using LinkCard.Dao;
using System.Data.SqlClient;

namespace LinkCard.ShopCP.Reports
{
    public partial class report : System.Web.UI.Page
    {
        public string sFromDate ="";
        public string sToDate = "";
        
        public DateTime dFromDate = new DateTime(1900, 1, 1);
        public DateTime dToDate = new DateTime(2099, 1, 1);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null)
            {
                Response.Redirect("~/login.aspx");
            }
            else if (ConvertUtility.ToInt32(Session["SHOPADMIN"]) == 0)
            {
                // Không đủ quyền thực hiện chức năng này
                //Response.Redirect("~/error.aspx");
                Response.Redirect("/message_box.aspx?msg=5");
            }
            if (!IsPostBack)
            {
                sFromDate = FromDate.Value;
                sToDate = ToDate.Value;
                if (sFromDate == "") sFromDate = DateTime.Today.AddMonths(-1).ToString("yyyy/MM/dd");
                if (sToDate == "") sToDate = DateTime.Today.ToString("yyyy/MM/dd");
                getReportOverview();

            }
        }

        private void getReportOverview()
        {
            Int64 nSupplierID = getSupplierID();
            var db = new TopLinkEntities();

            System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
            dateInfo.ShortDatePattern = "dd/MM/yyyy";

            if (sFromDate != "")
            {
                sFromDate += " 00:00:00";
                dFromDate = Convert.ToDateTime(sFromDate, dateInfo);

            }
            if (sToDate != "")
            {
                sToDate += " 23:59:59";
                dToDate = Convert.ToDateTime(sToDate, dateInfo);
            }

            var _payLog = db.PayLog.Where(t => t.SupplierID == nSupplierID 
                                            && t.PayDate >= dFromDate 
                                            && t.PayDate <= dToDate)
                                                                    .ToList();
            decimal totalValue = 0;
            int totalCash = 0;
            int totalpoint = 0;
            decimal totalCashValue = 0;
            decimal totalPointValue = 0;
            foreach (LinkCard.Model.PayLog pay in _payLog)
            {
                totalValue += (decimal)pay.PayPriceAfterDis;
                if (pay.PayMethod == 1 || pay.PayMethod == 0)
                {
                    totalCash += 1;
                    totalCashValue += (decimal)pay.PayPriceAfterDis;
                }
                else
                {
                    totalpoint += 1;
                    totalPointValue += (decimal)pay.PayPriceAfterDis;
                }
            }
            this.TotalValue.Text = Utility.sDecimal(Convert.ToDecimal(totalValue.ToString()));
            this.TotalCash.Text = totalCash.ToString();
            this.TotalPoint.Text = totalpoint.ToString();
            this.TotalTransaction.Text = _payLog.Count.ToString();
            this.TotalCashAmount.Text = Utility.sDecimal(totalCashValue);
            this.TotalPointAmount.Text = Utility.sDecimal(totalPointValue);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            sFromDate = FromDate.Value;
            sToDate = ToDate.Value;

            getReportOverview();
        }
        protected Int64 getSupplierID()
        {
            try
            {

                if (Convert.ToInt32(Session["SHOPADMIN"]) == 1) // quanr ly cua hang
                {
                    return ConvertUtility.ToInt64(Session["USER"]);
                }
                else if (Convert.ToInt32(Session["SHOPADMIN"]) == 0)
                {
                    DataTable dt = LinkCard.AdminCP.Common.DataHelper.SelectTable("SELECT SupplierID FROM [ShopStaff] WHERE Username ='" + Session["USER"].ToString() + "'", false);
                    if (dt.Rows.Count > 0)
                    {
                        return ConvertUtility.ToInt64(dt.Rows[0]["SupplierID"]);
                    }
                    else
                    {
                        return 0;
                    }
                }
                else
                {
                    return 0;
                }
            }
            catch
            {
                return 0;
            }
        }
    }
}